﻿#ifndef L_REPLY_HPP
#define L_REPLY_HPP

#include "reply.hpp"
#include <iostream>
#include "l_utility.hpp"

namespace http {
    namespace server3 {

        static inline void l_set_reply(lua_State *L, reply* rep) {
            lua_getglobal(L,__WEBQUEUE_TABLE); // push table t on stack
            lua_pushlightuserdata(L,rep); // push value v on stack
            // stack[-2] = t; stack[-1] = v
            // so t[key] = req
            lua_setfield(L,-2,__WEBQUEUE_REPLY);
            lua_pop(L,1); // pop table
        }

        static inline reply * l_get_reply(lua_State* L){
            reply *rep;
            lua_getglobal(L,__WEBQUEUE_TABLE); // push table t on stack
            lua_getfield(L,-1,__WEBQUEUE_REPLY);
            rep = (reply*)lua_touserdata(L,-1); // value is on top
            lua_pop(L,2); // pop table and value
            return rep;
        }
        int l_register_reply(lua_State* L);
    }
}
#endif  //L_REPLY_HPP
